﻿namespace EFS.Common.Config.Components.Elements
{
	/// <summary>
	/// Configuration element collection
	/// </summary>
	public class ElementCollection : System.Configuration.ConfigurationElementCollection
	{
		/// <summary>
		/// Add element
		/// </summary>
		/// <param name="element"></param>
		public void Add(EFS.Common.Config.Components.Elements.Element element)
		{
			BaseAdd(element);
		}

		/// <summary>
		/// Delete element
		/// </summary>
		/// <param name="element"></param>
		public void Delete(System.String elementName)
		{
			BaseRemove(elementName);
		}

		/// <summary>
		/// Check contains
		/// </summary>
		/// <param name="element"></param>
		public System.Boolean Contains(System.String elementName)
		{
			return null != this[elementName];
		}

		/// <summary>
		/// Indexer
		/// </summary>
		/// <param name="index"></param>
		/// <returns></returns>
		public EFS.Common.Config.Components.Elements.Element this[System.Int32 index]
		{
			get
			{
				// get from base
				return base.BaseGet(index) as EFS.Common.Config.Components.Elements.Element;
			}
			set
			{
				// check already set on index
				if (base.BaseGet(index) != null)
				{
					// remove
					base.BaseRemoveAt(index);
				}

				// add
				this.BaseAdd(index, value);
			}
		}

		/// <summary>
		/// Create new element
		/// </summary>
		/// <returns></returns>
		protected override System.Configuration.ConfigurationElement CreateNewElement()
		{
			return new EFS.Common.Config.Components.Elements.Element();
		}

		/// <summary>
		/// Get element key
		/// </summary>
		/// <param name="element"></param>
		/// <returns></returns>
		protected override object GetElementKey(System.Configuration.ConfigurationElement element)
		{
			return ((EFS.Common.Config.Components.Elements.Element)element).Name;
		}
	}
}
